<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\OrdersTarifsTermsModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Model\OrdersSharedTarifModel;
use mbilling\BillingBundle\Model\OrdersVpsTarifModel;
use mbilling\BillingBundle\Entity\ordersTarifsTerms;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\OrdersTarifsTermsForm;

class OrdersTarifsTermsController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $tarifsTerms_model = new OrdersTarifsTermsModel($em);
        $sharedTarif_model = new OrdersSharedTarifModel($em);
        $vpsTarif_model = new OrdersVpsTarifModel($em);
        
        $tarifs = array();
        $tarifsTerms = $tarifsTerms_model->findAll();
        foreach($tarifsTerms as $tarifTerms)
        {
            if ($tarifTerms->getTarifTypeId() == '1')
                $tarifs['1'][$tarifTerms->getId()] = $sharedTarif_model->findByPK($tarifTerms->getTarifId())->getName();
            else if ($tarifTerms->getTarifTypeId() == '2')
                $tarifs['2'][$tarifTerms->getId()] = $vpsTarif_model->findByPK($tarifTerms->getTarifId())->getName();
        }
        return $this->render('BillingBundle:Orders_tarifs_terms:index.html.twig', array('tarifsTerms' => $tarifsTerms, 'tarifs' => $tarifs));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $tarifsTerms_model = new OrdersTarifsTermsModel($em);
        $tarifTerms = new ordersTarifsTerms();
        //$tarifsTerms_model->create($tarifTerms->initDefaultEntity());
        //return new RedirectResponse($this->generateUrl('orders_tarifs_terms_show'));
        $form = $this->createForm(new OrdersTarifsTermsForm, $tarifTerms);
        
        $sharedTarif_model = new OrdersSharedTarifModel($em);
        $sharedTarifs = $sharedTarif_model->findAll();
        $sharedTarifs_string = array();
        foreach($sharedTarifs as $sharedTarif)
        {
            $sharedTarifs_string[$sharedTarif->getId()] = $sharedTarif;
        }

        $vpsTarif_model = new OrdersVpsTarifModel($em);
        $vpsTarifs = $vpsTarif_model->findAll();
        $vpsTarifs_string = array();
        foreach($vpsTarifs as $vpsTarif)
        {
            $vpsTarifs_string[$vpsTarif->getId()] = $vpsTarif;
        }

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $tarifsTerms_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('9');//страница сроки и тарифы
                $log->setObjectId($tarifTerms->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание сроков и тарифов
                $log_model->create($log);
                return new RedirectResponse($this->generateUrl('orders_tarifs_terms_show'));
            }
        }
        return $this->render('BillingBundle:Orders_tarifs_terms:create.html.twig', array('form' => $form->createView(),
                                                                                         'sharedTarifs' => $sharedTarifs_string,
                                                                                         'vpsTarifs' => $vpsTarifs_string));
    }
    public function updateAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $tarifsTerms_model = new OrdersTarifsTermsModel($em);
        $tarifTerms = $tarifsTerms_model->findByPK($id);

        $form = $this->createForm(new OrdersTarifsTermsForm, $tarifTerms);

        $sharedTarif_model = new OrdersSharedTarifModel($em);
        $sharedTarifs = $sharedTarif_model->findAll();
        $sharedTarifs_string = array();
        foreach($sharedTarifs as $sharedTarif)
        {
            $sharedTarifs_string[$sharedTarif->getId()] = $sharedTarif;
        }

        $vpsTarif_model = new OrdersVpsTarifModel($em);
        $vpsTarifs = $vpsTarif_model->findAll();
        $vpsTarifs_string = array();
        foreach($vpsTarifs as $vpsTarif)
        {
            $vpsTarifs_string[$vpsTarif->getId()] = $vpsTarif;
        }

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $tarifsTerms_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('9');//страница сроки и тарифы
                $log->setObjectId($tarifTerms->getId());//изменяемый объект
                $log->setActionTypeId('2');//изменение сроков и тарифов
                $log_model->create($log);
                return new RedirectResponse($this->generateUrl('orders_tarifs_terms_show'));
            }
        }
        return $this->render('BillingBundle:Orders_tarifs_terms:create.html.twig', array('form' => $form->createView(),
                                                                                         'sharedTarifs' => $sharedTarifs_string,
                                                                                         'vpsTarifs' => $vpsTarifs_string));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $tarifsTerms_model = new OrdersTarifsTermsModel($em);
        $tarifsTerms_model->removeByPK($id);

        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();

        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('9');//страница сроки и тарифы
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление сроков и тарифов
        $log_model->create($log);
        return new RedirectResponse($this->generateUrl('orders_tarifs_terms_show'));
    }
}